<?php

namespace backend\controllers;

use Yii;
use common\models\projectpaid\ProjectPaid;
use common\models\projectpaid\ProjectPaidSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use common\models\projectaccounttype\ProjectAccounttype;
use common\models\projectmoney\ProjectMoney;
use common\models\AdminLog\AdminLog;
use zhongkefu\zhongkefu;
use zhongkefu\helpers\Utils;
use yii\web\UploadedFile;

/**
 * ProjectPaidController implements the CRUD actions for ProjectPaid model.
 */
class ProjectPaidController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all ProjectPaid models.
     * @return mixed
     */
    public function actionIndex($id)
    {
        $searchModel = new ProjectPaidSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams, $id);
        $model_money = ProjectMoney::find()->where(['pr_id' => $id])->asArray()->all();

        $keys = array_column($model_money, 'name');
        $model_money = array_combine($keys, $model_money);

        
        $types = ProjectAccounttype::find()->all();
        $model_moneytot = ProjectMoney::find()->where(['pr_id' => $id, 'name' => '总计'])->asArray()->one();
        
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'types' => $types,
            'category_id' => $id,
            'model_money' => $model_money,
            'model_moneytot' => $model_moneytot,
        ]);
    }

    /**
     * Displays a single ProjectPaid model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {  
        $model = $this->findModel($id);
        $model->time = date('Y-m-d H:i', $model->time);
        $model->attachment = json_decode($model->attachment, true)['oldname'];
        return $this->render('view', [
            'model' => $model,
        ]);
    }

    /**
     * Creates a new ProjectPaid model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate($cid)
    {
        $model = new ProjectPaid();

        if ($model->load(Yii::$app->request->post())) {
            $model->attachment = UploadedFile::getInstance($model, 'attachment');
            if($model->attachment){
                $siteRoot = Yii::$app->params['articlefile'];
                $json = Utils::uploadfile($_FILES['ProjectPaid']['tmp_name']['attachment'],$_FILES['ProjectPaid']['name']['attachment'],$siteRoot);
            
                $model->attachment = $json;
            }
            $model->time = strtotime(Yii::$app->request->post()['ProjectPaid']['time']);
            $model->category = $cid;
            if ($model->save()) {
                return $this->redirect(['view', 'id' => $model->id]);
            }
        } else {
            $model->category = $cid;
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing ProjectPaid model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post())) {
            
            $model->attachment = UploadedFile::getInstance($model, 'attachment');
            if($model->attachment){
                $siteRoot = Yii::$app->params['articlefile'];
                $json = Utils::uploadfile($_FILES['ProjectPaid']['tmp_name']['attachment'],$_FILES['ProjectPaid']['name']['attachment'],$siteRoot);
                $model->attachment = $json;
            }
            //若为更新文件，则为原数据库中文件名称
            if (!$model->attachment) {
                $new = $this->findModel($id);
                $model->attachment = $new->attachment;
            }
            
            $model->time = strtotime($model->time);
            if ($model->save()) {
                return $this->redirect(['view', 'id' => $model->id]);
            }
        } else {
            $model->time = date('Y-m-d H:i', $model->time);
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing ProjectPaid model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $cid = ProjectPaid::find()->where(['id' => $id])->asArray()->one()['category'];
        $this->findModel($id)->delete();
        return $this->redirect(['index', 'id' => $cid]);
    }

    /**
     * Finds the ProjectPaid model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return ProjectPaid the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = ProjectPaid::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    
    /**
     * 删除按钮
     * If update is successful, the browser will be redirected to the 'index' page.
     * @return mixed
     */
    public function actionAlldelete()
    {

        $artname='';
        if(isset(zhongkefu::$app->request->post()["checkboxid"])){
            $ids = zhongkefu::$app->request->post()["checkboxid"];
            $ids = explode(',',$ids);
            foreach($ids as $val){
                $model = ProjectPaid::findOne(['id' => $val]);
                $artname .= $model->description.",";
            }
            $artname = substr($artname,0,-1);        
            foreach($ids as $id){
                $this->findModel($id)->delete();
            }
            AdminLog::addlog(yii::$app->user->id, "批量删除已支付现金 ".$artname);
            return true;
        }
    }
    
}
